<template>
  <div class="main">
     <div class="header">
        <input v-model="text" placeholder="添加ToDo" @keyup.enter="handleEnter">
     </div>
     <div class="item" v-for="item in list" :key="item.id">
        <div class="item-left">
          <input type="checkbox" v-model="item.status" @change="change">
        </div>
        <div :class="item.status ? 'select item-conter' : 'item-conter' ">{{item.text}}</div>
        <div class="item-right" @click="handleDel(item.id)">x</div>
     </div>
     <div class="footer" v-if="allList.length > 0">
      <div class="active">{{completedList.length}} item left</div>
      <div class="tab">
        <span :class="tab==='allList' ? 'tabAction' : ''" @click="handleTab('allList')">All</span>
        <span :class="tab==='activeList' ? 'tabAction' : ''" @click="handleTab('activeList')">Active</span>
        <span :class="tab==='completedList' ? 'tabAction' : ''" @click="handleTab('completedList')">Completed</span>
      </div>
      <div class="selectDel" @click="handleSelectDel">
          Clear completed
      </div>
     </div>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
      list: [], 
      text: '',
      tab: "allList",
      allList: [],
    }
  },
  computed: {
    activeList() {
      return this.allList.filter((item) => !item.status);
    },
    completedList() {
      return this.allList.filter((item) => item.status);
    },
  },
  methods: {
    change(){
      this.list = this[this.tab]
    },
    handleTab(name){
      this.tab = name
      this.list = this[name]
    },
    handleSelectDel(){
      this.list = this.allList.filter(i=> !i.status)
      this.allList = this.allList.filter(i=> !i.status)
    },
    handleDel(id){
      this.list =  this.list.filter((item)=>{return item.id !== id})
      this.allList = this.allList.filter((item)=>{return item.id !== id})
    },
    handleEnter(){
      if(this.text){
        this.allList.push(
          {
            text: this.text,
            status: false,
            id: new Date().getTime(),
          }
        )
        this.text = ""
        this.handleTab(this.tab)
      }
    },
  },
}
</script>

<style>
  .main{
    width: 400px;
    margin:0 auto;
  }
  .header input {
    width: 400px;
    height: 30px;
    text-indent: 10px;
    border-radius: 5px;
  }
  .item{
    position: relative;
    width: 400px;
    height: 30px;
    line-height: 30px;
    margin:10px auto;
    border: 1px solid #ccc;
    display: flex;
  }
  .item:hover .item-right{
    display: block;
  }
  .item-left input {
    width: 20px;
    height: 20px;
    border-radius: 5px;
    margin: 5px 10px;
  }
  .item-conter{
    width: 330px;
    text-align: left;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .item-right{
    display: none;
    cursor:pointer;
    color: red;
    position: absolute;
    top: 0px;
    right: 10px;
    line-height: 30px;
    font-size: 20px;
  }
  .select{
    text-decoration: line-through;
    color: #999;
  }
  .footer{
    box-sizing: border-box;
    display: flex;
    font-size: 14px;
    line-height: 30px;
    padding: 0 10px;
  }
  .tabAction{
    border: 1px solid #ccc;
  }
  .tab{
    flex: 1;
  }
  .tab span{
    padding: 0 7px;
    margin:  0 3px;
    cursor:pointer;
  }
  .selectDel{
    cursor:pointer;
  }
</style>
